<!-- 正文开始 -->
<!-- 正文开始 -->
<style>
    .w-120 {
        width: 115px;
    }

    .w-500 {
        width: 500px !important;
    }

    .title-txt {
        padding: 0 0 20px 30px;
        font-weight: 600;
        font-size: 18px;
    }

    .conentStyle {
        padding: 10px;
        box-sizing: border-box;
    }

    .conentBg {
        padding: 20px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #fff;
        min-height: 660px;
    }

    .borderNone {
        border: none;
    }

    .formBg {
        background-color: #F7F8FA;
        padding: 20px;
    }
    .layui-table-cell{
        height: auto;
    }
    .layui-table-cell img{
        width: 80px;
        height: 80px;
    }

    .import {
        opacity: 0;
        position: absolute;
        width: 69px;
        height: 38px;
        margin-left: -10px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <!--<div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="subjectSetBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="subjectSetBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>-->
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="subjectSetBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="subjectSetTable" lay-filter="subjectSetTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="subjectSetTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="subjectSetForm">
    <form lay-filter="subjectSetForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">所属分类</label>
            <input name="subjectCategoryId" value="" type="hidden" lay-verify="required">
            <div class="layui-input-inline mr0" id="subjectCategoryId" style="width: 716px;"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">科目类型</label>
            <div class="layui-input-block">
                <select name="subjectType" lay-verify="required">
                        <option value="">请选择科目类型</option>
                        <option value="property">资产</option>
                        <option value="liabilities">负债</option>
                        <option value="rights">权益</option>
                        <option value="cost">成本</option>
                        <option value="loss">损失</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">助记码</label>
            <div class="layui-input-block">
                <input name="mnemonicCode" placeholder="请输入助记码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">余额方向</label>
            <div class="layui-input-block">
                <select name="balanceDirection" lay-verify="required">
                    <option value="">请选择余额方向</option>
                    <option value="borrow">借</option>
                    <option value="loan">贷</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">币种</label>
            <div class="layui-input-block">
                <select name="currency" lay-verify="required">
                    <option value="">请选择币种</option>
                        <option value="rmb">人民币</option>
                        <option value="usd">美元</option>
                        <option value="jpy">日元</option>
                        <option value="krw">韩元</option>
                        <option value="other">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">默认单位</label>
            <div class="layui-input-block">
                <input name="unitName" placeholder="请输入默认单位" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">辅助账类型</label>
            <div class="layui-input-block">
                <select name="subsidiaryAccount">
                        <option value="">请选择辅助账类型</option>
                        <option value="department">部门核算</option>
                        <option value="project">项目核算</option>
                        <option value="person">个人往来</option>
                        <option value="customer">客户往来</option>
                        <option value="supplier">供应商往来</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账页格式</label>
            <div class="layui-input-block">
                <select name="ledgerFormat">
                         <option value="">请选择辅助账类型</option>
                        <option value="amount">金额式</option>
                        <option value="quantityAmount">数量金额式</option>
                        <option value="foreignCurrency">外币金额式</option>
                        <option value="currencyQuantity">外币数量式</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea  name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="subjectSetFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','tableTreeDj','newTree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var tableTree = layui.tableTreeDj;
        var newTree = layui.newTree;
        // 渲染表格
        var insTb = table.render({
            elem: '#subjectSetTable',
            url: config.base + 'finance/financeSet/admin/subjectSet/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'name', sort: true, title: '名称'}
                ,{field: 'mnemonicCode', sort: true, title: '助记码'}
                ,{field: 'subjectCategoryName', sort: true, title: '所属分类'}
                ,{field: 'subjectTypeDesc', sort: true, title: '所属类型'}
                ,{field: 'balanceDirectionDesc', sort: true, title: '余额方向'}
                ,{field: 'isDeleted', sort: true, title: '是否启用',templet:function (d) {
                        if(d.isDeleted){
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }

                    }}
                ,{field: 'remark', sort: true, title: '描述'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#subjectSetTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(subjectSetTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('finance/financeSet/admin/subjectSet/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('subjectSetTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('finance/financeSet/admin/subjectSet/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('subjectSetTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }
        //监听排序
        table.on('sort(subjectSetTable)', function(obj){
            table.reload('subjectSetTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#subjectSetBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: '65px',
                title: data ? '修改科目设置' : '添加科目设置',
                content: $('#subjectSetForm').html(),
                success: function () {
                    //加载物料分类树级菜单
                    var arr2tree;
                    admin.req('finance/financeSet/admin/subjectCategory/list', {limit:9999}, function (res) {
                        for (let i = 0; i < res.data.length; i++) {
                            res.data[i].children = [];
                        }
                        arr2tree = xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                    }, 'get', false);

                    var selectedData = function (treeData, targetId) {

                        for (var k = 0; k < treeData.length; k++) {
                            var nodeId = treeData[k].id;
                            if (targetId == nodeId) {
                                treeData[k].selected = true;
                                return;
                            } else {
                                if (treeData[k].children && treeData[k].children != '') {
                                    selectedData(treeData[k].children, targetId);
                                }
                            }
                        }
                    }

                    if (data) {
                        selectedData(arr2tree, data.subjectCategoryId);
                    }

                    var smlSelectInstans = xmSelect.render({
                        el: '#subjectCategoryId',
                        model: {label: {type: 'text'}},
                        radio: true,
                        clickClose: true,
                        filterable: true,
                        tree: {
                            show: true,
                            strict: false,
                            expandedKeys: [-1],
                        },
                        tips: '顶级分类',
                        prop: {
                            name: 'name',
                            value: 'id',
                            children: 'children'
                        },
                        data() {
                            //填充数据
                            return arr2tree;
                        },
                        on(data) {
                            let parentId = data.arr.length > 0 ? data.change[0].id : -1;
                            $("input[name='subjectCategoryId']").val(parentId);
                        }
                    });

                    if (data){
                        var option = $("form select[name='subjectType']").find("option");
                        layui.each(option, function(index, item){
                            if(data.subjectType  == item.text){
                                data.subjectType = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='currency']").find("option");
                        layui.each(option, function(index, item){
                            if(data.currency  == item.text){
                                data.currency = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='subsidiaryAccount']").find("option");
                        layui.each(option, function(index, item){
                            if(data.subsidiaryAccount  == item.text){
                                data.subsidiaryAccount = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='ledgerFormat']").find("option");
                        layui.each(option, function(index, item){
                            if(data.ledgerFormat  == item.text){
                                data.ledgerFormat = item.value;
                            }
                        });
                    }

                    form.val('subjectSetForm', data);
                    // 表单提交事件
                    form.on('submit(subjectSetFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'finance/financeSet/admin/subjectSet/update' : 'finance/financeSet/admin/subjectSet/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('subjectSetTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('finance/financeSet/admin/subjectSet/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#subjectSetBtnExport').click(function () {
            var checkRows = table.checkStatus('subjectSetTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>